package com.lkc.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.lkc.domain.ResponseResult;
import com.lkc.domain.entity.Menu;
import com.lkc.domain.entity.User;
import com.lkc.domain.vo.MenuTreeVo;
import com.lkc.domain.vo.RouterTreeVo;
import com.lkc.enums.AppHttpCodeEnum;
import com.lkc.exception.SystemException;
import com.lkc.mapper.MenuMapper;
import com.lkc.mapper.UserMapper;
import com.lkc.service.SystemService;
import com.lkc.utils.BeanCopyUtils;
import com.lkc.utils.JwtUtil;
import com.lkc.utils.security.LoginUser;
import com.lkc.utils.tree.TreeUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;

/**
 * @Title: SystemServiceImpl
 * @Author 椰咩咩
 * @Package com.lkc.service.impl
 * @Date 2022/12/23 13:53
 * @description: 系统（system）业务层实现类
 */
@Service
public class SystemServiceImpl implements SystemService {

    @Resource
    private UserMapper userMapper;

    @Resource
    private MenuMapper menuMapper;

    @Override
    public ResponseResult login(User user) {
        if (!StringUtils.hasText(user.getId().toString())) {
            //提示 必须要传用户名
            throw new SystemException(AppHttpCodeEnum.REQUIRE_USERNAME);
        }
        if (!StringUtils.hasText(user.getPassword())) {
            //提示 必须要传用户名
            throw new SystemException(AppHttpCodeEnum.REQUIRE_PASSWORD);
        }
        //根据用户名和密码查询User
        LambdaQueryWrapper<User> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(User::getId, user.getId());
        wrapper.eq(User::getPassword, user.getPassword());
        User loginUser = userMapper.selectOne(wrapper);
        //判断是否登录成功
        if (Objects.isNull(loginUser)) {
            throw new SystemException(AppHttpCodeEnum.LOGIN_ERROR);
        }
        //返回给前端的数据不需要password

        loginUser.setPassword(null);
        //登录成功生成Token返回到前端
        String token = JwtUtil.createJWT(user.getId().toString(), 1000*60 * 60 * 2L);
        Map<Object, Object> loginVoMap = new HashMap<>();
        loginVoMap.put("user", loginUser);
        loginVoMap.put("token", token);
        return ResponseResult.okResult(loginVoMap);
    }

    @Override
    public ResponseResult getMenu() {
        //根据对应的User查询响应的menu列表
        User user = LoginUser.getUser();
        LambdaQueryWrapper<Menu> wrapper = new LambdaQueryWrapper<>();
        //查询对应的权限
        wrapper.in(Menu::getPermission, user.getRole(), 2);
        wrapper.in(Menu::getType, "D", "M");
        wrapper.orderByAsc(Menu::getOrderNum);
        List<Menu> menus = menuMapper.selectList(wrapper);
        List<MenuTreeVo> menuTreeVos = BeanCopyUtils.copyBeanList(menus, MenuTreeVo.class);
        List<MenuTreeVo> menuTree = TreeUtils.generateTrees2(menuTreeVos);
        return ResponseResult.okResult(menuTree);
    }

    @Override
    public ResponseResult getRouters() {
        //根据对应的User查询响应的menu
        User user = LoginUser.getUser();
        LambdaQueryWrapper<Menu> wrapper = new LambdaQueryWrapper<>();
        //查询对应的权限
        wrapper.in(Menu::getPermission, user.getRole(), 2);
        wrapper.in(Menu::getType, "C", "M");
        List<Menu> menus = menuMapper.selectList(wrapper);
        List<RouterTreeVo> routerTreeVos = BeanCopyUtils.copyBeanList(menus, RouterTreeVo.class);
        List<RouterTreeVo> routerTree = TreeUtils.generateTrees2(routerTreeVos);
        return ResponseResult.okResult(routerTree);
    }
}
